package com.yc.javaee.servlet.d0618;
import com.google.gson.Gson;
import com.yc.common.util.DBHelper;
import com.yc.javaee.bean.Result;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Map;
@WebServlet(name = "GetUserServlet", value = "/getUser.s")
public class GetUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String sql = "select * from tbl_user where id=?";
        Map<String, Object> user = null;
        try {
            user = DBHelper.selectOneMap(sql, id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Result res;
        if (user == null) {
            res = new Result(0, "查询用户失败", null);
        } else {
            res = new Result(1, "查询用户成功", user);
        }
        Gson gson = new Gson();
        String json = gson.toJson(res);
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().append(json);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
